﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EjemploSVN.Models;

namespace EjemploSVN.Controllers
{ 
    public class StudentsController : BaseController
    {
        private IStudentsRepo repo;
        
        public StudentsController(IStudentsRepo repo)
        {
            this.repo = repo;
        }
        
        //private ModelContext db = new ModelContext();

        //
        // GET: /Students/

        public ViewResult Index()
        {
            return View(this.repo.GetEmpiezanConA());
        }

        //
        // GET: /Students/Details/5

        public ViewResult Details(int id)
        {
            Student student = this.repo.Get(id);
            return View(student);
        }

        //
        // GET: /Students/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Students/Create

        [HttpPost]
        public ActionResult Create(Student student)
        {
            if (ModelState.IsValid)
            {
                using (IUnitOfWork uow = this.UoWFactory.Current)
                {
                    this.repo.Add(student);
                    uow.Commit();
                    return RedirectToAction("Index");
                }
            }

            return View(student);
        }
        
        //
        // GET: /Students/Edit/5
 
        public ActionResult Edit(int id)
        {
            Student student = this.repo.Get(id);
            return View(student);
        }

        //
        // POST: /Students/Edit/5

        [HttpPost]
        public ActionResult Edit(Student student)
        {
            if (ModelState.IsValid)
            {
                using (IUnitOfWork uow = this.UoWFactory.Current)
                {
                    this.repo.Update(student);
                    uow.Commit();
                    return RedirectToAction("Index");
                }
            }
            return View(student);
        }

        //
        // GET: /Students/Delete/5
 
        public ActionResult Delete(int id)
        {
            Student student = this.repo.Get(id);
            return View(student);
        }

        //
        // POST: /Students/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            using (IUnitOfWork uow = this.UoWFactory.Current)
            {
                Student student = this.repo.Get(id);
                this.repo.Remove(student);
                uow.Commit();
                return RedirectToAction("Index");
            }
        }

    }
}